package com.herench.bs.core;


import com.herench.bs.support.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * Created by ThinkPad on 2017/8/14.
 */
@Component
public class SimpleMessageProcessor implements MessageProcessor {

    @Autowired
    private CleverClinicContext cleverClinicContext;

    @Override
    public ResponseMessage handler(RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = null;
        MessageHandler handler = null;
        if (!cleverClinicContext.isAlive()) {
            cleverClinicContext.init();
        }
        String funcode = requestMessage.getFuncode();
        Assert.hasLength(funcode, "funcode 参数为空.");
        handler = cleverClinicContext.getMessageHandlerServicePool().acquire(funcode);
        if (handler == null) {
            throw new RuntimeException(funcode + "所对应的handler没注册.");
        }
        responseMessage = handler.handler(requestMessage);
        return responseMessage;
    }
}
